﻿using System.IO;

namespace MediaFS.Data
{
    public sealed class MediaFile:MemoryFile
    {
        public MediaFile(MemoryFolder parent, string name, string actualPath) : base(parent, name)
        {
            ActualPath = actualPath;
            var fileInfo = new FileInfo(ActualPath);
            Size = fileInfo.Length;
            Attributes = fileInfo.Attributes;
            LastAccessTime = fileInfo.LastAccessTime;
            LastWriteTime = fileInfo.LastWriteTime;
            CreationTime = fileInfo.CreationTime;
        }

        public override long Size { get; set; }

        public string ActualPath { get; set; }

        public override uint Write(long offset, byte[] buffer)
        {
            var fs = File.OpenWrite(ActualPath);
            fs.Seek(offset, SeekOrigin.Begin);
            fs.Write(buffer, 0, buffer.Length);
            return (uint) buffer.Length;
        }

        public override uint Read(long offset, byte[] buffer)
        {
            var fs = File.OpenRead(ActualPath);
            fs.Seek(offset, SeekOrigin.Begin);
            return (uint)fs.Read(buffer, 0, buffer.Length);
        }

        public static MediaFile Create(MemoryFolder folder, string actualFileName)
        {
            return new MediaFile(folder, Path.GetFileName(actualFileName), actualFileName);
        }
    }
}
